package cn.gson.school.controller.xitong;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.gson.school.dao.xitong.UserDao;
import cn.gson.school.entity.xitong.RoleEntity;
import cn.gson.school.entity.xitong.UserEntity;
import cn.gson.school.mappers.xitong.UserMapper;
import lombok.extern.log4j.Log4j;

@Log4j
@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private UserDao userDao;
	

	/**
     * 超级管理员id
     */
    @Value("${system.super.user.userId}")
    Long superUserId;

	@RequestMapping("list")
	@ResponseBody
	public Map<String, Object> index(UserEntity user, @RequestParam(name = "page", defaultValue = "1") int pageNum,
			@RequestParam(name = "rows", defaultValue = "20") int pageSize) {
		// 查询数据
		PageHelper.startPage(pageNum, pageSize);
		List<UserEntity> pageData = userMapper.userlist(user);
		PageInfo<UserEntity> info = new PageInfo<>(pageData);
		// 组装返回结果
		Map<String, Object> obj = new HashMap<String, Object>();
		obj.put("total", info.getTotal());
		obj.put("rows", info.getList());
		return obj;

	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 */
	@GetMapping("delete")
	@ResponseBody
	@Transactional
	public AjaxResult delete(Long id) {
		Map<String, Object> obj = new HashMap<String, Object>();
		obj.put("success", false);
		UserEntity user;
		if (id != null && (user = userDao.findOne(id)) != null) {
			userDao.delete(user);
			obj.put("success", true);
		} else {
			obj.put("message", "此用户不存在！");
		}
		return new AjaxResult();
	}

	/**
	 * 获取信息
	 * 
	 * @param id
	 * @param roles
	 * @param model
	 * @return
	 */
	@GetMapping("xt-adduser")
	public String form(Model model) {

		// 角色下拉框的值
		List<RoleEntity> rolelist = userMapper.roleList();
		System.out.println(rolelist);
		model.addAttribute("rolelist", rolelist);
		return "xitong/xt-adduser";
	}

	/**
	 * 添加
	 * 
	 * @param user
	 * @param roleId
	 * @param br
	 * @return
	 */
	@PostMapping("save")
	@ResponseBody
	@Transactional
	public JSONObject save(@Valid UserEntity user, @RequestParam(name = "roleName") Long roleId, BindingResult br) {
		System.out.println(user);
		System.out.println("你进来了码？");
		JSONObject obj = new JSONObject();
		obj.put("success", false);

		if (br.hasErrors()) {
			obj.put("message", "校验失败！");
			obj.put("error", br.getFieldErrors());
		}

		RoleEntity role = new RoleEntity();// 添加角色
		role.setRoleId(roleId); // 赋值给实体类
		List<RoleEntity> roles = new ArrayList<>(); // 用一个集合吧实体类装起来
		roles.add(role); // 把role添加到roles中
		user.setRoles(roles);// 在吧roles添加到User中

		/*
		 * DeptEntity dept = new DeptEntity();// 添加部门 dept.setDeptId(deptInfo);//
		 * 把查询到的ID给实体类 emps.setDept(dept);// 在把dept添加到员工中
		 * 
		 * user.setUserPass(DigestUtils.md5Hex(user.getUserPass()));// 密码加密
		 * emps.setHiredate(new Date());// new 一个系统时间
		 *
		 * 
		 * UserInfoEntity emp1 = empDao.save(emps);
		 * user.setUserinfoEntity(empDao.findOne(emp1.getUserInfoId()));
		 * System.out.println("empId" + empDao.findOne(emp1.getUserInfoId()));
		 */
		user.setDelete(1);
		user.setRegistTime(new Date());
		userDao.save(user);

		obj.put("success", true);
		return obj;

	}

	@RequestMapping("/password/reset")
	@Transactional
	@ResponseBody
	public AjaxResult saves(Long id) {
		UserEntity u = userDao.findOne(id);
		System.out.println("sssss" + userDao.findOne(id));
		u.setUserPass(DigestUtils.md5Hex("123456789"));
		userDao.save(u);
		return new AjaxResult();

	}

}
